<template>
  <div class="login-form-wrapper">
    <div class="login-form-title">注册</div>
    <div class="login-form-sub-title">魔方赛事网</div>
    <div class="login-form-error-msg">{{ errorMessage }}</div>
    <a-form
      ref="loginForm"
      class="login-form"
      layout="vertical"
      @submit="handleSubmit"
    >
      <a-form-item
        field="nickName"
        hide-label>
        <a-input
          v-model="regForm.nickName"
          placeholder="用户名"
          allow-clear>
          <template #prefix>
            <icon-user />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item
        field="email"
        :rules="[
          {
            match: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/,
            message: $t('login.form.email.errMsg'),
          },
          {
            required: true,
            message: $t('login.form.email.errMsg'),
          },
        ]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input
          v-model="regForm.email"
          :placeholder="$t('login.form.email.placeholder')"
        >
          <template #prefix>
            <icon-at />
          </template>
        </a-input>
        <a-button @click="sendEmail">发送验证码</a-button>
      </a-form-item>
      <a-form-item
        field="password"
        :rules="[{ required: true, message: $t('login.form.password.errMsg') }]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input-password
          v-model="regForm.password"
          :placeholder="$t('login.form.password.placeholder')"
          allow-clear
        >
          <template #prefix>
            <icon-lock />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item
        field="password"
        :rules="[{ required: true, message: $t('login.form.password.errMsg') }]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input-password
          v-model="regForm.confrimPassword"
          placeholder="确认密码"
          allow-clear
        >
          <template #prefix>
            <icon-lock />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item
        field="gender"
        hide-label>
        <a-select v-model="regForm.gender" placeholder="性别" allow-clear>
          <a-option value="0">女</a-option>
          <a-option value="1">男</a-option>
          <template #prefix>
            <icon-user-group />
          </template>
        </a-select>
      </a-form-item>
      
      <a-form-item
        field="schoolId"
        hide-label>
        <a-auto-complete :data="schoolData" @change="handleSchoolSearch" :style="{width:'360px'}" placeholder="请选择学校">
          <template #prefix>
            <icon-trophy />
          </template>
        </a-auto-complete>
      </a-form-item>

      <a-form-item
        field="verifyCode"
        :rules="[
          { required: true, message: $t('login.form.verifyCode.errMsg') },
        ]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input
          v-model="regForm.verifyCode"
          :placeholder="$t('login.form.verifyCode.placeholder')"
        >
          <template #prefix>
            <icon-code-square />
          </template>
        </a-input>
      </a-form-item>
      <a-space :size="16" direction="vertical">
        <a-button 
          @click="handleRegSubmit"
          type="primary" html-type="submit" long :loading="loading">
          立即注册
        </a-button>
        <a-button @click="goLogin" type="text" long class="login-form-register-btn">
          已有账号？去登录
        </a-button>
      </a-space>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, onMounted } from 'vue';
  import { useRouter } from 'vue-router';
  import { Message } from '@arco-design/web-vue';
  import axios from 'axios';

  const router = useRouter();

  const regForm = ref({
    nickName: '',
    email: '',
    password: '',
    confrimPassword: '',
    gender: '',
    school: '',
    verifyCode: ''
  })

  const schoolData = ref([])
  const originSchoolData = ref([])

  const handleSchoolSearch = (v) => {
    console.log(v);
    axios.post(`${import.meta.env.VITE_API_Admin_URL}school/list?searchField=school_name&search=${v}`).then(res => {
      console.log(res);
      schoolData.value = []
      originSchoolData.value = []
      res.forEach(r => {
        schoolData.value.push(r.schoolName)
        originSchoolData.value.push(r)
      })
    })
  }

  onMounted(() => {
    console.log("aaaa");
    
  })

  const sendEmail = () => {
    axios.post(`common/email/reg-send`, {
      email:regForm.value.email
    }).then(res => {
      Message.info({
        id: 'myInfo',
        content: `发送验证码成功`,
        duration: 2000
      })
    })
  }

  const handleRegSubmit =  () => {
    axios.post(`user/register`, {
      "againPassword": regForm.value.confrimPassword,
      "email": regForm.value.email,
      "emailVerifyCode": regForm.value.verifyCode,
      "gender": regForm.value.gender,
      "nickName": regForm.value.nickName,
      "password": regForm.value.password,
      "schoolId": originSchoolData.value[0].schoolId,
    }).then(res => {
      Message.info({
        id: 'myInfo',
        content: `注册成功`,
        duration: 2000
      })
      router.push('/login')
    })
  }

  const goLogin = () => {
    router.push('/login')
  }

</script>

<style lang="less" scoped>
  .login-form {
    &-wrapper {
      width: 320px;
    }

    &-title {
      color: var(--color-text-1);
      font-weight: 500;
      font-size: 24px;
      line-height: 32px;
    }

    &-sub-title {
      color: var(--color-text-3);
      font-size: 16px;
      line-height: 24px;
    }

    &-error-msg {
      height: 32px;
      color: rgb(var(--red-6));
      line-height: 32px;
    }

    &-password-actions {
      display: flex;
      justify-content: space-between;
    }

    &-register-btn {
      color: var(--color-text-3) !important;
    }
  }

  .verify-code {
    margin-inline-start: 10px;
  }
</style>
